package com.longshen.dubbo.api.restful;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.longshen.dubbo.api.anno.LoginRequired;
import com.longshen.dubbo.api.base.ErrorCode;
import com.longshen.dubbo.api.base.RestResult;
import com.longshen.dubbo.api.model.AdminTrade;
import com.longshen.dubbo.api.restful.vo.ModeId;
import com.longshen.dubbo.api.restful.vo.TradeListOut;
import com.longshen.dubbo.api.restful.vo.TradeVO;
import com.longshen.dubbo.api.restful.vo.TreadePageIn;
import com.longshen.dubbo.api.service.AdminTradeService;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @ClassName:  AdminTradeController   
 * @Description:交易权限管理服务   
 * @author: longshen
 * @date:   2019年10月25日 下午6:07:33
 */
@Slf4j
@RequestMapping(value = "/trade")
@RestController
@LoginRequired
public class AdminTradeController {

	@Autowired
	private AdminTradeService adminTradeService;

	

	
	// 分页查询
		@RequestMapping(value = "/list", method = RequestMethod.POST)
		public RestResult list(@RequestBody TreadePageIn in) {

			Page<AdminTrade> pages = adminTradeService.queryPage(in.getPagesize(), in.getPagenum(), in.getTradecode(),in.getTradename());
			if (pages != null && pages.getPageSize() > 0) {
				TradeListOut out = new TradeListOut();
				List<TradeVO> list = new ArrayList<TradeVO>();
				for (AdminTrade t : pages) {
					TradeVO vo = new TradeVO();
					vo.setFlag(t.getFlag());
					vo.setId(t.getId());
					vo.setTradecode(t.getTradeCode());
					vo.setTradedesc(t.getTradeDesc());
					vo.setTradename(t.getTradeName());
					list.add(vo);
				}
				out.setCurrentsize(pages.getPageSize());
				out.setTotalsize((int) pages.getTotal());
				out.setList(list);
				RestResult rest = RestResult.success();
				rest.setData(out);
				return rest;
			} else {
				RestResult rest = RestResult.newInstance();
				rest.setErrorCode(ErrorCode.E_000004);
				return rest;
			}
		}
		// 添加

		@RequestMapping(value = "/add", method = RequestMethod.POST)
		public RestResult add(@RequestBody TradeVO in) {
			try {

				AdminTrade add = new AdminTrade();
				add.setFlag(in.getFlag());
				add.setTradeCode(in.getTradecode());
				add.setTradeDesc(in.getTradedesc());
				add.setTradeName(in.getTradename());
				adminTradeService.add(add);

				return RestResult.success();
			} catch (DuplicateKeyException e) {
				log.error("添加角色异常", e);
				RestResult rest = RestResult.newInstance();
				rest.setErrorCode(ErrorCode.E_000006);
				rest.setMessage(rest.getMessage() + in.getTradecode());
				return rest;
			} catch (Exception e) {
				return RestResult.fail(ErrorCode.E_999999);
			}
		}

		// 删除
		@RequestMapping(value = "/del", method = RequestMethod.POST)
		public RestResult del(@RequestBody ModeId in) {

			adminTradeService.delById(in.getId());
			return RestResult.success();
		}

		// 修改
		@RequestMapping(value = "/mod", method = RequestMethod.POST)
		public RestResult mod(@RequestBody TradeVO in) {

			AdminTrade mod = new AdminTrade();
			mod.setFlag(in.getFlag());
			mod.setTradeCode(in.getTradecode());
			mod.setTradeDesc(in.getTradedesc());
			mod.setTradeName(in.getTradename());
			mod.setId(in.getId());
			adminTradeService.mod(mod);
			return RestResult.success();
		}
	
	

}
